;
(function () {
  window.rbacVues = new Vue({
    el: '#userL_rbac',
    data: {
      pid: 0,
      index: 0,
      memberType: 'A',
      memberData: [],
      memberActive: 'WX',
      odata: [],
      adata: [],
      csid: null,
      menberList: '',
      newAddInnput: false,
      isShowAddInput: true,
      newEditName: '',
      menberTypeId: '',
      menberTypeParentId: '',
      adminOrgId: '',
      memberOrgId: '',
      isShowAddBtn: false, //新增按钮
      permissions: JSON.parse(window.localStorage.getItem('permissions')),
      level: 0,
    },
    computed: {
      treeList() {
        let arr = [];
        let parent, children;
        parent = this.adata
        children = this.adata
        for (let i = parent.length - 1; i >= 0; i--) {
          let item0 = parent[i];
          for (let j = 0; j < children.length; j++) {
            let item1 = children[j];
            if (item0.id == item1.parentId) {
              item0.children.push(item1)
            }
          }
        }
        for (let k = 0; k < parent.length; k++) {
          if (Number(parent[k].parentId) > Number(this.csid)) {
            parent.splice(k, 1)
            k--
          }
        }
        arr = parent
        let count = 0;
        guierzi(arr, count)

        if (arr.length == 0 && this.memberActive == 'WX') {
          this.isShowAddBtn = true
        } else {
          this.isShowAddBtn = false
        }

        return arr
      }
    },
    methods: {
      //新建
      sureAdd() {
        var that = this
        hlhttp.post({
          url: 'position',
          data: {
            pid: that.pid,
            short_name: that.newEditName
          },
          success: (res) => {
            that.getPidData(that.pid)
          }
        })
      },
      //获取身份信息
      getStwxdd() {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: this.pid,
            member_type: this.memberType,
            show_member: 'N'
          },
          success: (res) => {
            if (!res.subOrgs.length) return;
            res.subOrgs.map(item => {
              if (item.orgType == 'WX') {
                item.index = 0
              }
              if (item.orgType == 'DD') {
                item.index = 1
              }
            })

            res.subOrgs = res.subOrgs.sort(hlArrayUtil.compareAvg('index'))
            this.memberActive = res.subOrgs[0].orgType

            if (res.subOrgs.length == 1) {
              res.subOrgs[0].shortName = '组织架构'
            }

            // 默认点击为第一项
            this.pid = res.subOrgs[0].id
            this.csid = res.subOrgs[0].id
            this.memberData = res.subOrgs;
            this.getStwxddData()
          }
        })

      },
      //根据身份信息id调用
      getStwxddData(fn) {
        var self = this;
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: this.pid,
            member_type: this.memberType,
            show_member: 'N'
          },
          success: (data) => {
            data.subOrgs.map(item => {
              item.flag = true;
              item.oflag = false;
              item.parentId = this.pid;
              item.children = [];
              this.odata.unshift(item)
            })

            if (this.memberActive == 'WX' && this.odata.length == 0) {
              self.isShowAddInput = true
            } else {
              self.isShowAddInput = false
            }
            this.odata = hlArrayUtil.removeRepeat(this.odata, 'id');
            this.odata = this.odata.sort(hlArrayUtil.compareAvg('id'));
            this.adata = JSON.parse(JSON.stringify(this.odata))
            if (fn) fn();
          }
        })
      },
      getPidData(id, type, localId) {
        if (type == 'del') {
          for (var i = 0; i < this.odata.length; i++) {
            if (this.odata[i].id == localId) {
              this.odata.splice(i, 1)
            }
          }
        }
				if (id == this.pid) return
        this.pid = id;
        this.getStwxddData()
      },
      // 获取成员（点击组织）
      getmenberType(id, parentId, adminOrgId, memberOrgId) {
        this.menberTypeId = id
        this.menberTypeParentId = parentId
        this.adminOrgId = adminOrgId
        this.memberOrgId = memberOrgId
        userLists.menberitem = null
        hlhttp.get({
          url: 'organization/member',
          data: {
            org_head_id: adminOrgId,
            org_member_id: memberOrgId
          },
          success: (data) => {
            userLists.menberList = data
            userLists.menberTypeId = this.menberTypeId
          }
        })
      },
      // 获取成员（添加成员）
      getmenberType1() {
        hlhttp.get({
          url: 'organization/member',
          data: {
            org_head_id: this.adminOrgId,
            org_member_id: this.memberOrgId
          },
          success: (data) => {
            userLists.menberList = data
            userLists.menberTypeId = this.menberTypeId
          }
        })
      },
      setmemberType(type, text) {
        if (this.memberOrgId) {
          userLists.setSC = true
          userLists.SCtext = text
          this.SCmembertype = type
        }
      },
      // 确定是客户还是员工
      sureSetmenberType() {
        if (this.menberTypeId) {
          hlhttp.put({
            url: 'organization/member_type',
            data: {
              organization_id: this.menberTypeId,
              member_type: this.SCmembertype
            },
            success: (res) => {
              this.getPidData(this.menberTypeParentId)
            }
          })
        }
      },
      //切换导航
      tabNav(item) {
        this.memberActive = item.orgType;
        userLists.wxOrdd = item.orgType
        this.pid = item.id;
        this.csid = item.id;
        this.odata = [];
        this.adata = [];
        userLists.menberList = '',
          userLists.menberTypeId = '',
          $('.slideTop').css('display', 'none')

        this.getStwxddData()
        if (item.orgType == 'WX') {
          userLists.source = 'S'
        } else {
          userLists.source = 'D'
          this.isShowAddInput = false
        }
        userLists.reset()
      },
      closeAddInput() {
        this.newAddInnput = false
        this.newEditName = null
      },
      openAddInput() {
        this.newAddInnput = true
        this.newEditName = null
      }
    },
    mounted() {
      this.getStwxdd();
    }
  })

  function guierzi(arr, count) {
    count++
    arr.map(item => {
      item.level = count
      if (item.children.length) {
        guierzi(item.children, count)
      }
    })
  }

}())
